﻿using KamRemakeRemake.GameStates;
using KamRemakeRemake.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace KamRemakeRemake {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class KaMRemake : Game {
        private GameState State { get; set; }

        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        public Viewport Viewport {
            get {
                return this.GraphicsDevice.Viewport;
            }
        }

        public KaMRemake() {
            this.Content.RootDirectory = "Content";
            this.IsMouseVisible = false;

            // Default graphics settings
            this.graphics = new GraphicsDeviceManager(this);
            this.graphics.IsFullScreen = false;
            //this.graphics.PreferredBackBufferWidth = 1600;
           // this.graphics.PreferredBackBufferHeight = 900;

            this.State = new MenuState(this);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent() {
            // Create a new SpriteBatch, which can be used to draw textures.
            this.spriteBatch = new SpriteBatch(GraphicsDevice);

            Cursor.Instance.LoadContent(this.Content);
            this.State.LoadContent(this.Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent() {
            this.Content.Unload();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime) {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape)) {
                this.Exit();
            }

            this.State.Update(gameTime);
            Cursor.Instance.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime) {
            this.GraphicsDevice.Clear(Color.Black);

            this.spriteBatch.Begin();
            this.State.Draw(gameTime, spriteBatch, Content);
            Cursor.Instance.Draw(gameTime, this.spriteBatch, Content);
            base.Draw(gameTime);
            this.spriteBatch.End();
        }

        /// <summary>
        /// Initializes a new state
        /// </summary>
        public void SetState(GameState newState) {
            this.State = newState;
            newState.LoadContent(this.Content);
        }
    }
}
